<?php
namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Validate;
use think\Db;
use app\admin\model\HospitalLevelModel;
use app\admin\model\DrugStoreModel;
class DrugstoreController extends AdminBaseController
{
	/*
		*药店分类列表
	*/
	public function drugstorecate(){
		$res=Db::name("drugstore_level")->select();
		return $this->fetch('Drugstore/drugstorecate',[
				'res'=>$res
			]);
	}
	/*
		*药店分类添加
	*/
	public function drugstorecateadd(){
		return $this->fetch('Drugstore/drugstorecateadd');
	}
	/*
		*药店分类添加执行
	*/
	public function drugstorecateadddo(){
		if($this->request->isPost()){
			$data = $this->request->param();
			$res=Db::name("drugstore_level")->where('level',$data['level'])->find();
			if($res==null){
					$level = ['level'=>$data['level'],'create_time'=>time()];
				    $res1=Db::name("drugstore_level")->insert($level);
					if($res1 == 1 ){
						$this->success("添加成功");
					}else{
						$this->error("添加失败");
					}
			}else{
				$this->error("该分类已存在");
			}
		}
	}
	/*
		*药店分类删除
	*/
	public function drugstorecatedetele(){
		$data = $this->request->param();
        $res=Db::name("drugstore_level")->where('id',$data['id'])->delete();
    	if($res ==1){
    		$this->success('删除成功');
    	}else{
    		$this->error('删除失败');
    	}
	}
	/*
		*药店分类修改
	*/
	public function drugstorecateupdate(){
		$data = $this->request->param();
		$res=Db::name("drugstore_level")->where('id',$data['id'])->find();
		return $this->fetch('Drugstore/drugstorecateupdate',[
		  	   'res'=>$res
		]);
	}
    /*
     * 药店分类修改执行
     * */
	public function drugstorecateupdatedo(){
		if($this->request->isPost()){
			$data = $this->request->param();
			$res=Db::name("drugstore_level")->where('id',$data['id'])->update(['level'=>$data['level']]);
			if($res == 1){
				$this->success('修改成功',url('Drugstore/drugstorecate'));
			}else{
				$this->error('修改失败',url('Drugstore/drugstorecate'));
			}
		}
	}
	/*
	 *药店列表
	 * */
	public function  Drugstorelist(){
        $res=Db::name("drugstore")->select();
        return $this->fetch('Drugstore/drugstorelist',[
            'res'=>$res
        ]);
    }
    /*
     *药店添加
     * */
    public function  Drugstoreadd(){
        $res=Db::name('drugstore_level')->select();
        return $this->fetch('Drugstore/Drugstoreadd',[
            'res'=>$res
        ]);
    }
    /*
     * 药店添加执行
     * */
    public function  Drugstoreadddo(){
        if($this->request->isPost()) {
            $data = $this->request->param();
            $drugstore=new DrugStoreModel();
            $drugstore->shopname=$data['shopname'];
            $drugstore->shoplevel=$data['shoplevel'];
            $drugstore->province=$data['province'];
            $drugstore->city=$data['city'];
            $drugstore->district=$data['district'];
            $drugstore->xcity=$data['xcity'];
            $drugstore->openimages=$data['openimages'];
            $drugstore->licenceimages=$data['licenceimages'];
            $drugstore->status=$data['status'];
            $drugstore->create_time=time();
            $res1=$drugstore->save();
            if($res1 == 1 ){
                $this->success("添加成功");
            }else{
                $this->error("添加失败");
            }
        }
    }
    /*
     * 文件上传
     * */
    public function filesinput(){
        $data = $this->request->param();
        $images=$data['images'];
        $base64_img = trim($images);
        $date=date("Y/m/d",time());
        $up_dir = 'upload/admin/';//存放在当前目录的upload文件夹下
        if(!file_exists($up_dir)){
            mkdir($up_dir,0777);
        }

        if(preg_match('/^(data:\s*image\/(\w+);base64,)/', $base64_img, $result)){
            $type = $result[2];
            if(in_array($type,array('pjpeg','jpeg','jpg','gif','bmp','png'))){
                $new_file = $up_dir.date('YmdHis').'.'.$type;
                if(file_put_contents($new_file, base64_decode(str_replace($result[1], '', $base64_img)))){
                    $img_path = str_replace('../../..', '', $new_file);
                    return $img_path;
                }else{
                    echo '图片上传失败</br>';

                }
            }else{
                //文件类型错误
                echo '图片上传类型错误';
            }

        }else{
            //文件错误
            echo '文件错误';
        }
    }
    /*
     * 药店删除
     * */
    public function Drugstoredelete(){
        $data = $this->request->param();
        $res=Db::name("drugstore")->where('id',$data['id'])->delete();
        if($res ==1){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    /*
     * 药店修改
     * */
    public  function Drugstoreupdate(){
        $data = $this->request->param();
        $res=Db::name('drugstore_level')->select();
        $res2=Db::name('drugstore')->where('id',$data['id'])->find();
        return $this->fetch('Drugstore/drugstoreupdate',[
            'res'=>$res,
            'res2'=>$res2
        ]);
    }
    /*
     * 药店修改执行
     * */
    public  function Drugstoreupdatedo(){
        if($this->request->isPost()){
            $data = $this->request->param();
            $datainfo=[
                'shopname'=>$data['shopname'],
                'shoplevel'=>$data['shoplevel'],
                'province'=>$data['province'],
                'city'=>$data['city'],
                'district'=>$data['district'],
                'xcity'=>$data['xcity'],
                //'openimages'=>$data['openimages'],
               // 'licenceimages'=>$data['licenceimages'],
                'status'=>$data['status'],
                'create_time'=>time(),
            ];
            $res=Db::name("drugstore")->where('id',$data['id'])->update($datainfo);
            if($res == 1){
                $this->success('修改成功',url('Drugstore/Drugstorelist'));
            }else{
                $this->error('修改失败',url('Drugstore/Drugstorelist'));
            }
        }
    }
    /*
     * 店长执行
     * */
    public function shopmanager(){
        $res=Db::name('shopmanager')->select();
        return $this->fetch('Drugstore/shopmanager',[
            'res'=>$res
        ]);
    }
    /*
     * 店长添加
     * */
    public function shopmanageradd(){
        $res=Db::name('drugstore')->select();
        return $this->fetch('Drugstore/shopmanageradd',[
            'res'=>$res
        ]);
    }
    /*
     * 店长添加执行
     * */
    public function shopmanageradddo(){
        if($this->request->isPost()){
            $data = $this->request->param();
            $datainfo=[
                'managername'=>$data['managername'],
                'shopname'=>$data['shopname'],
                'tel'=>$data['tel'],
                'identitycard'=>$data['identitycard'],
                'position'=>$data['position'],
                'openimages'=>$data['openimages'],
                'status'=>$data['status'],
                'create_time'=>time()
            ];
            $res=Db::name("shopmanager")->insert($datainfo);
            if($res == 1){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }
    }
    /*
     * 店长删除
     * */
    public function shopmanagerdetele(){
        $data = $this->request->param();
        $res=Db::name("shopmanager")->where('id',$data['id'])->delete();
        if($res ==1){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    /*
     * 店长修改
     * */
    public function shopmanagerupdate(){
        $res=Db::name('drugstore')->select();
        $data = $this->request->param();
        $res2=Db::name('shopmanager')->where('id',$data['id'])->find();
        return $this->fetch('Drugstore/shopmanagerupdate',[
            'res'=>$res,
            'res2'=>$res2
        ]);
    }
    /*
     * 店长修改执行
     * */
    public function shopmanagerupdatedo(){
        if($this->request->isPost()){
            $data = $this->request->param();
            $datainfo=[
                'managername'=>$data['managername'],
                'shopname'=>$data['shopname'],
                'tel'=>$data['tel'],
                'identitycard'=>$data['identitycard'],
                'position'=>$data['position'],
                'openimages'=>$data['openimages'],
                'status'=>$data['status'],
            ];
            $res=Db::name("shopmanager")->where('id',$data['id'])->update($datainfo);
            if($res == 1){
                $this->success('修改成功',url('Drugstore/shopmanager'));
            }else{
                $this->error('修改失败',url('Drugstore/shopmanager'));
            }
        }
    }
    /*
     * 店员列表
     * */
    public function shopassistant(){
        $res=Db::name('shopassistant')->select();
        return $this->fetch('Drugstore/shopassistant',[
            'res'=>$res
        ]);
    }
    /*
     *店员列表添加
     * */
    public function  shopassistantadd(){
        $res=Db::name('drugstore')->select();
        return $this->fetch('Drugstore/shopassistantadd',[
            'res'=>$res
        ]);
    }
    /*
     *店员列表添加执行
     * */
    public function  shopassistantadddo(){
        if($this->request->isPost()){
            $data = $this->request->param();
            $datainfo=[
                'managername'=>$data['managername'],
                'shopname'=>$data['shopname'],
                'tel'=>$data['tel'],
                'identitycard'=>$data['identitycard'],
                'position'=>$data['position'],
                'openimages'=>$data['openimages'],
                'status'=>$data['status'],
                'create_time'=>time()
            ];
            $res=Db::name("shopassistant")->insert($datainfo);
            if($res == 1){
                $this->success('添加成功');
            }else{
                $this->error('添加失败');
            }
        }
    }
    /*
    * 店长删除
    * */
    public function shopassistantdelete(){
        $data = $this->request->param();
        $res=Db::name("shopassistant")->where('id',$data['id'])->delete();
        if($res ==1){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
    /*
     * 店长修改
     * */
    public function shopassistantupdate(){
        $res=Db::name('drugstore')->select();
        $data = $this->request->param();
        $res2=Db::name('shopmanager')->where('id',$data['id'])->find();
        return $this->fetch('Drugstore/shopassistantupdate',[
            'res'=>$res,
            'res2'=>$res2
        ]);
    }
    /*
     * 店长修改执行
     * */
    public function shopassistantupdatedo(){
        if($this->request->isPost()){
            $data = $this->request->param();
            $datainfo=[
                'managername'=>$data['managername'],
                'shopname'=>$data['shopname'],
                'tel'=>$data['tel'],
                'identitycard'=>$data['identitycard'],
                'position'=>$data['position'],
                'openimages'=>$data['openimages'],
                'status'=>$data['status'],
            ];
            $res=Db::name("shopassistant")->where('id',$data['id'])->update($datainfo);
            if($res == 1){
                $this->success('修改成功',url('Drugstore/shopmanager'));
            }else{
                $this->error('修改失败',url('Drugstore/shopmanager'));
            }
        }
    }
}

